package com.longxiao.manager.web;

import com.longxiao.entity.ClientVersion;
import com.longxiao.para.VersionSearchPara;
import com.longxiao.service.VersionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yusong on 2017/6/2.
 * 版本管理
 */
@Controller
@RequestMapping(value = "version")
public class VersionController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private VersionService versionService;

    /*
    *分页搜索用户
    */
    @RequestMapping(value = "/list")
    public String list(@ModelAttribute("pageParam") VersionSearchPara versionSearchPara, HttpServletRequest request) {
        List<ClientVersion> records = versionService.listByPage(versionSearchPara);
        Integer count = versionService.count(versionSearchPara);
        versionSearchPara.setTotalCount(count);
        request.setAttribute("pageParam",versionSearchPara);
        request.setAttribute("records",records);
        return "version/list";
    }

    /*
   *开始新增用户
   *跳转到新增用户页面
   */
    @RequestMapping(value = "/addBefore")
    private String addBefore(){
        return "version/add";
    }

    /*
     *新增版本
     */
    @RequestMapping(value = "/doAdd")
    @ResponseBody
    public Map<String, String> doAdd(@ModelAttribute("property") ClientVersion clientVersion) {
        logger.info("doAdd|新增版本信息|"+clientVersion);
        Map<String, String> resultMap = new HashMap<String, String>();
        String message = "";
        boolean result = false;
        try {
            versionService.addVersion(clientVersion);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("statusCode", "300");
            message = "操作失败";
        }
        if (result) {
            resultMap.put("statusCode", "200");
            message = "操作成功";
        }
        resultMap.put("message", message);
        resultMap.put("callbackType", "closeCurrent");
        return resultMap;
    }


    /*
    *开始新增用户
    *跳转到新增用户页面
    */
    @RequestMapping(value = "/updateBefore/{versionId}")
    private String updateBefore(@PathVariable("versionId") Integer versionId,HttpServletRequest request){
        request.setAttribute("record", versionService.getById(versionId));
        return "version/update";
    }

    /*
     *新增版本
     */
    @RequestMapping(value = "/doUpdate")
    @ResponseBody
    public Map<String, String> doUpdate(@ModelAttribute("property") ClientVersion clientVersion) {
        logger.info("doAdd|修改版本信息|"+clientVersion);
        Map<String, String> resultMap = new HashMap<String, String>();
        String message = "";
        boolean result = false;
        try {
            versionService.update(clientVersion);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("statusCode", "300");
            message = "操作失败";
        }
        if (result) {
            resultMap.put("statusCode", "200");
            message = "操作成功";
        }
        resultMap.put("message", message);
        resultMap.put("callbackType", "closeCurrent");
        return resultMap;
    }

    @RequestMapping(value = "/doDelete/{versionId}")
    @ResponseBody
    public Map<String, String> doDelete(@PathVariable("versionId") Integer versionId) {
        Map<String, String> paraMap = new HashMap<String, String>();
        String message = "";
        boolean result = false;
        try {
            versionService.delete(versionId);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
            paraMap.put("statusCode", "300");
            message = "操作失败";
        }
        if (result) {
            paraMap.put("statusCode", "200");
            message = "操作成功";
        }
        paraMap.put("message", message);
        paraMap.put("callbackType", "refreshCurrent");
        return paraMap;
    }

}
